#!/bin/bash

# To be run on the instance to install the fibonacci server.
# If aws_env set, add leading -
. /mnt/keys/credentials
AWS_ENV=$1
if [ X$AWS_ENV = X ]; then ENV=""; else ENV="-$AWS_ENV"; fi
USER=aws
BIN=/home/$USER/fibonacci
LOG=/var/log/fib.log
IMAGE=$AWS_USER-ami-fibonacci${ENV}.img
MANIFEST=$IMAGE.manifest.xml
export RUBYLIB=/mnt/lib

echo "export RUBYLIB=/home/$USER/lib" >>/home/$USER/.bash_profile
echo "export AWS_HOME=/home/$USER" >>/home/$USER/.bash_profile

echo "installing fibonacci"
if `[ ! -d $BIN ]`
then mkdir $BIN
fi

echo "Welcome to EC2 $AWS_USER Fibonacci Image $AWS_ENV" >/etc/motd

echo "creating log file"
touch $LOG
chown $USER $LOG
chmod ugo+w $LOG

echo "creating control and init scripts"
cp -r /mnt/fibonacci/* $BIN
cp -r /mnt/lib /home/$USER
cp -r /mnt/tools /home/$USER
cp /mnt/fibonacci/fib /etc/init.d
/sbin/chkconfig --add fib

cd /mnt

echo "bundling"
rm -f $IMAGE
ec2-bundle-vol --volume / --prefix $IMAGE --destination /mnt --arch i386 --size 10240 --cert /mnt/keys/$AWS_CERT --privatekey /mnt/keys/$AWS_PRIVATE_KEY --user $AWS_USER_ID

echo "uploading"
ec2-upload-bundle --manifest /mnt/$MANIFEST --bucket $AWS_BUCKET --access-key $AWS_ACCESS_KEY --secret-key $AWS_SECRET_KEY


echo "registering image"
cd /mnt/tools
./deregister-image $AWS_BUCKET/$MANIFEST
./register-image $AWS_BUCKET/$MANIFEST
./make-image-public $AWS_BUCKET/$MANIFEST

